/*
Copyright 2014, KISSY v1.47
MIT Licensed
build time: May 22 12:24
*/
KISSY.add("editor/plugin/list-utils/cmd",["editor","../list-utils"],function(o,u){function r(g){this.type=g}function v(g,d){var e,h,c,b=d.blockLimit,a=d.elements;if(!b)return!1;if(a)for(c=0;c<a.length&&(e=a[c])&&e[0]!==b[0];c++)if(t[h=e.nodeName()]&&h===g)return e.css("list-style-type");return!1}var q=u("editor"),p=u("../list-utils"),t={ol:"insertOrderedList",ul:"insertUnorderedList"},w=q.RangeType,y=q.ElementPath,x=q.Walker,z=o.UA,m=o.Node,s=o.DOM,A=/^h[1-6]$/;r.prototype={constructor:r,changeListType:function(g,
d,e,h,c){for(var b=p.listToArray(d.root,e,void 0,void 0,void 0),a=[],g=0;g<d.contents.length;g++){var f=d.contents[g];if((f=f.closest("li",void 0))&&f[0]&&!f.data("list_item_processed"))a.push(f),f._4eSetMarker(e,"list_item_processed",!0,void 0)}f=new m(d.root[0].ownerDocument.createElement(this.type));f.css("list-style-type",c);for(g=0;g<a.length;g++)c=a[g].data("listarray_index"),b[c].parent=f;for(var e=p.arrayToList(b,e,null,"p"),i,c=e.listNode.childNodes.length,g=0;g<c&&(i=new m(e.listNode.childNodes[g]));g++)i.nodeName()===
this.type&&h.push(i);d.root.before(e.listNode);d.root.remove()},createList:function(g,d,e,h){var c=d.contents,g=d.root[0].ownerDocument,b=[];if(1===c.length&&c[0][0]===d.root[0]){var a=new m(g.createElement("div"));c[0][0].nodeType!==s.NodeType.TEXT_NODE&&c[0]._4eMoveChildren(a,void 0,void 0);c[0][0].appendChild(a[0]);c[0]=a}d=d.contents[0].parent();for(a=0;a<c.length;a++)d=d._4eCommonAncestor(c[a].parent(),void 0);for(a=0;a<c.length;a++)for(var f=c[a],i;i=f.parent();){if(i[0]===d[0]){b.push(f);break}f=
i}if(!(1>b.length)){c=new m(b[b.length-1][0].nextSibling);a=new m(g.createElement(this.type));a.css("list-style-type",h);for(e.push(a);b.length;)e=b.shift(),h=new m(g.createElement("li")),A.test(e.nodeName())?h[0].appendChild(e[0]):(e._4eCopyAttributes(h,void 0,void 0),e._4eMoveChildren(h,void 0,void 0),e.remove()),a[0].appendChild(h[0]),z.ie||h._4eAppendBogus(void 0);c[0]?a.insertBefore(c,void 0):d.append(a)}},removeList:function(g,d,e){function h(a){if((j=new m(i[a?"firstChild":"lastChild"]))&&
!(j[0].nodeType===s.NodeType.ELEMENT_NODE&&j._4eIsBlockBoundary(void 0,void 0))&&(k=d.root[a?"prev":"next"](x.whitespaces(!0),1))&&!(j[0].nodeType===s.NodeType.ELEMENT_NODE&&k._4eIsBlockBoundary({br:1},void 0)))j[a?"before":"after"](g.get("document")[0].createElement("br"))}for(var c=p.listToArray(d.root,e,void 0,void 0,void 0),b=[],a=0;a<d.contents.length;a++){var f=d.contents[a];if((f=f.closest("li",void 0))&&!f.data("list_item_processed"))b.push(f),f._4eSetMarker(e,"list_item_processed",!0,void 0)}f=
null;for(a=0;a<b.length;a++)f=b[a].data("listarray_index"),c[f].indent=-1;for(a=f+1;a<c.length;a++)if(c[a].indent>Math.max(c[a-1].indent,0)){b=c[a-1].indent+1-c[a].indent;for(f=c[a].indent;c[a]&&c[a].indent>=f;)c[a].indent+=b,a++;a--}var i=p.arrayToList(c,e,null,"p").listNode,j,k;h(!0);h(void 0);d.root.before(i);d.root.remove()},exec:function(g,d){var e=g.getSelection(),h=e&&e.getRanges();if(h&&!(1>h.length)){for(var c=e.getStartElement(),b,c=new q.ElementPath(c),a=v(this.type,c),c=e.createBookmarks(!0),
f=[],i={};0<h.length;){b=h.shift();var j=b.getBoundaryNodes(),k=j.startNode,l=j.endNode;k[0].nodeType===s.NodeType.ELEMENT_NODE&&"td"===k.nodeName()&&b.setStartAt(j.startNode,w.POSITION_AFTER_START);l[0].nodeType===s.NodeType.ELEMENT_NODE&&"td"===l.nodeName()&&b.setEndAt(j.endNode,w.POSITION_BEFORE_END);j=b.createIterator();for(j.forceBrBreak=!1;k=j.getNextParagraph();)if(!k.data("list_block")){k._4eSetMarker(i,"list_block",1,void 0);var l=new y(k),m=l.elements,p=!1,o=l.blockLimit,n;for(b=m.length-
1;0<=b&&(n=m[b]);b--)if(t[n.nodeName()]&&o.contains(n)){o.removeData("list_group_object");(b=n.data("list_group_object"))?b.contents.push(k):(b={root:n,contents:[k]},f.push(b),n._4eSetMarker(i,"list_group_object",b,void 0));p=!0;break}p||(l=o||l.block,l.data("list_group_object")?l.data("list_group_object").contents.push(k):(b={root:l,contents:[k]},l._4eSetMarker(i,"list_group_object",b,void 0),f.push(b)))}}for(h=[];0<f.length;)b=f.shift(),a?t[b.root.nodeName()]&&(b.root.css("list-style-type")===d?
this.removeList(g,b,i):b.root.css("list-style-type",d)):t[b.root.nodeName()]?this.changeListType(g,b,i,h,d):(q.Utils.clearAllMarkers(i),this.createList(g,b,h,d));var r=this;for(b=0;b<h.length;b++)n=h[b],a=function(a,b){var c=b[a?"prev":"next"](x.whitespaces(!0),1);c&&c[0]&&c.nodeName()===r.type&&c.css("list-style-type")===d&&(c.remove(),c._4eMoveChildren(b,a?!0:!1,void 0))},a(void 0,n),a(!0,n);q.Utils.clearAllMarkers(i);e.selectBookmarks(c)}}};return{ListCommand:r,queryActive:v}});
